package gold.digger;

import gold.utils.InputUtil;

import java.util.PriorityQueue;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC871 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int minRefuelStops(int target, int startFuel, int[][] stations) {
            int need = target - startFuel;
            int ans = 0;
            int p = 0;
            PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> b - a);
            while (p < stations.length || !queue.isEmpty()) {
                // 油够了，加ans次
                if (need <= 0) {
                    return ans;
                }
                // 油能达到的加油站，都加做备选加油站
                while (p < stations.length && stations[p][0] <= startFuel) {
                    queue.add(stations[p++][1]);
                }
                // 还有加油站，但是油不够到达这个加油站，也无油可加
                if (p < stations.length && stations[p][0] > startFuel && queue.isEmpty()) {
                    return -1;
                }
                // 能加油，就加此时最多的
                if (!queue.isEmpty()) {
                    int add = queue.poll();
                    startFuel += add;// 扩大能达到的边界
                    need -= add;// 减少需求量
                    ans += 1;// 加油一次
                }
            }
            return need <= 0 ? ans : -1;
        }
    }

    public void run() {
        int[] arr = InputUtil.toIntegerArray("[1,2,3]");
        System.out.println(new Solution().toString());
    }

    public static void main(String[] args) throws Exception {
        LC871 an = new LC871();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
